#include <QtGui/QApplication>
#include <QTextCodec>
#include <QSettings>
#include <QTranslator>
#include <QDebug>
#include <QObject>

#include "definitions.h"
#include "formmain.h"
#include "appfactory.h"
#include "singleapplication.h"
#ifdef HAVE_DBUS
#include "dbusadaptor.h"
#endif

QSettings* AppFactory::settings;
QWebSettings* AppFactory::webSettings;

QIcon UiFactory::stateNewIcon;
QIcon UiFactory::stateNormalIcon;
QIcon UiFactory::stateErrorIcon;

int main(int argc, char *argv[]) {
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

#ifdef Q_WS_MAC
    QSettings::setDefaultFormat(QSettings::IniFormat);
#endif

	// Obtain username for local server key/name
	// The logic behind is to have unique (per session)
    // ID for rssguard to ensure only one app is started
    // Let's suppose it's not possible to start 2 sessions
    // for one user on Windows (username only). But check
    // the DISPLAY on UN*X where it's possible. Examples:
	//		mac: pvanek_/tmp/launch-sXYeRy/org.macosforge.xquartz:0
	//		linux: pvanek_:0
    QString uname;
#ifdef Q_OS_UNIX
	// Mac, linux, and the other unices.
    uname = qgetenv("USER");
    if (!uname.isEmpty())
        uname += "_" + qgetenv("DISPLAY");
#endif
#ifdef Q_WS_WIN
	// Windows.
    uname = qgetenv("USERNAME");
#endif
    if (uname.isEmpty()) {
		qDebug() << QString("%1 Cannot get uname. Using default").arg(APP_ID);
        uname = "RSSGuard";
    }

    SingleApplication rssApp(argc, argv, uname);

    if (rssApp.isRunning()) {
		return 0;
	}

	// These settings needs to be set before any QSettings object.
    rssApp.setApplicationName(APP_NAME);
    rssApp.setApplicationVersion(APP_VERSION);
    rssApp.setOrganizationName(APP_AUTHORS);
    rssApp.setOrganizationDomain(APP_URL);

	AppFactory::setupSettings();
	AppFactory::checkSettings();
	UiFactory::setupStateIcons(":/graph/resources/graphics/state-new.png",
							   ":/graph/resources/graphics/state-normal.png",
							   ":/graph/resources/graphics/state-problem.png");

    QTranslator qtTranslator;
	QString lang = AppFactory::settings->value("currentLanguage", "").toString();

	qtTranslator.load(lang, APP_LANG_PATH);
    rssApp.installTranslator(&qtTranslator);

	qDebug() << QString("%1 %2 %3 is launching.").arg(APP_ID,
													  rssApp.applicationName(),
													  rssApp.applicationVersion());

    FormMain rssGui;

#ifdef HAVE_DBUS
    new DBusAdaptor(&rssGui);
	// Connect to D-Bus and register as an object.
    QDBusConnection::sessionBus().registerService(DBUS_SERVICE);
    QDBusConnection::sessionBus().registerObject(DBUS_PATH, &rssGui);
#endif

	if (AppFactory::settings->value("startMinimized", false).toBool())
        rssGui.hide();
    else
        rssGui.show();

	if (AppFactory::settings->value("checkForUpdate", false).toBool())
		rssGui.checkForUpdate();

	if (AppFactory::settings->value("updateOnStart", true).toBool())
        rssGui.updateAllFeeds();

    return rssApp.exec();
}
